package com.algorithms.sorts;

public abstract class Sort {
	protected String sortName;
	
	protected int[] data;
	protected int length;
	
	protected int numOfProcess = 0;
	protected int numOfCopy = 0;
	
	public Sort(){
		this.data = new int[0];
		this.length = 0;
	}
	
	public Sort(int[] dataArray, int dataLength) {
		this.data = dataArray;
		this.length = dataLength;
	}
	
	public void setSortName(String sortName){
		this.sortName = sortName;
	}
	
	public abstract int[] sort();
	
	protected void swap(int i, int j){
		int buff = this.data[i];
		this.data[i] = this.data[j];
		this.data[j] = buff;
	}
	
	public void sortWithMeasure(){
		long startTime = System.nanoTime();
		this.sort();
		long duration = System.nanoTime() - startTime;
		System.out.println("do " + this.sortName + 
						" in " + (duration/1000) + "μs");
	}
	
	public void printElements(){
		// print out the result of sort process
		for (int i = 0; i < this.length; i++){
			System.out.print(this.data[i] + (i == this.length - 1 ? "" : ", "));
		}
		
		System.out.println();
		
		// print out the number of steps and copies
		System.out.println("steps: " + this.numOfProcess + ", " +
						"copies: " + this.numOfCopy);
	}
}
